//
//  PYPhotoAlbum.m
//  test
//
//  Created by Jim on 2018/2/6.
//  Copyright © 2018年 Jim. All rights reserved.
//

#import "PYPhotoAlbum.h"
#import "TZImagePickerController.h"
#import <Photos/Photos.h>

@implementation PYPhotoAlbum

+ (void)photoMaxCount:(NSInteger)maxCount didFinishBlock:(PYPhotoFinishBlock)photoFinishBlock
{
    TZImagePickerController *imagePickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:maxCount delegate:nil];
    
    imagePickerVC.allowTakePicture = NO;
    
    imagePickerVC.allowPickingImage = YES;
    
    imagePickerVC.allowPickingVideo = NO;
    
    [imagePickerVC setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        NSLog(@"photos = %@, assets = %@", photos, assets);
        
        py_dispatch_async_main_safe(^{
            
            if (photoFinishBlock) {
                
                photoFinishBlock(photos);
            }
        });
        
    }];
    
    [[PYPhotoAlbum rootViewController] presentViewController:imagePickerVC animated:YES completion:nil];

}

+ (void)videoDidFinishBlock:(PYVideoFinishBlock)videoFinishBlock
{
    TZImagePickerController *imagePickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    
    imagePickerVC.allowTakePicture = NO;
    
    imagePickerVC.allowPickingImage = NO;
    
    imagePickerVC.allowPickingVideo = YES;
    
    imagePickerVC.allowPickingMultipleVideo = NO;
    
    imagePickerVC.autoDismiss = NO;

    __weak typeof(imagePickerVC) weakImagePickerVC = imagePickerVC;
    
    [imagePickerVC setDidFinishPickingVideoHandle:^(UIImage *coverImage, id asset) {
        
        NSLog(@"coverImage = %@, asset = %@", coverImage, asset);
        PHAsset *phAsset = asset;
        if (phAsset.duration <= 60) {
            
            if (phAsset.mediaType == PHAssetMediaTypeVideo) {
                PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
                options.version = PHVideoRequestOptionsVersionOriginal;
                options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
                options.networkAccessAllowed = YES;
                
                options.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
                    
                    NSLog(@"progress = %f", progress);
                };
                
                PHImageManager *manager = [PHImageManager defaultManager];

                [manager requestAVAssetForVideo:phAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
                    
                    AVURLAsset *urlAsset = (AVURLAsset *)asset;
                    
                    NSURL *url = urlAsset.URL;
                    
                    NSLog(@"url = %@", url);
                    
                    py_dispatch_async_main_safe(^{
                        
                        if (videoFinishBlock) {
                            
                            videoFinishBlock(url, coverImage, phAsset.duration);
                            
                            [weakImagePickerVC dismissViewControllerAnimated:YES completion:nil];
                            
                        }
                    });
                    
                }];

            }
        } else {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择60s内的视频" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            
            [alertController addAction:okAction];
            
            [[PYPhotoAlbum rootViewController] presentViewController:alertController animated:YES completion:nil];
        }
        
    }];
    
    [imagePickerVC setImagePickerControllerDidCancelHandle:^{
       
        [weakImagePickerVC dismissViewControllerAnimated:YES completion:nil];

    }];
    
    [[PYPhotoAlbum rootViewController] presentViewController:imagePickerVC animated:YES completion:nil];

}

+ (UIViewController *)rootViewController
{
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    
    UIViewController *parent = rootVC;
    
    while ((parent = rootVC.presentedViewController) != nil ) {
        rootVC = parent;
    }
    
    while ([rootVC isKindOfClass:[UINavigationController class]]) {
        rootVC = [(UINavigationController *)rootVC topViewController];
    }
    
    return rootVC;
}

@end
